package com.xy.job.admin.common.config

import com.xy.job.core.api.response.ResponseCode
import com.xy.job.core.api.response.ResponseWrapper
import com.xy.job.core.api.response.error
import com.xy.job.core.exception.BizException
import jakarta.validation.ConstraintViolationException
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class GlobalExceptionHandler {

    private val log = LoggerFactory.getLogger(GlobalExceptionHandler::class.java)

    @ExceptionHandler(BizException::class)
    fun handleBizException(e: BizException): ResponseWrapper<Unit> {
        log.info("occurred biz exception, msg={}", e.message, e)
        return error(code = ResponseCode.BIZ_ERROR, message = e.message)
    }

    @ExceptionHandler(ConstraintViolationException::class)
    fun handleConstraintViolationException(e: ConstraintViolationException): ResponseWrapper<Unit> {
        return error(message = e.message)
    }

    @ExceptionHandler(RuntimeException::class)
    fun handleConstraintViolationException(e: RuntimeException): ResponseWrapper<Unit> {
        log.error("occurred system exception, msg={}", e.message, e)
        return error(message = e.message)
    }

}